#pragma once
#include <iostream>
using namespace std;
#include <unistd.h>

typedef void (*work_t)(int rfd);
typedef void (*task_t)(int rfd, pid_t pid);
void Printlog(int rfd, pid_t pid)
{
    cout << "pid:" << pid << ",rfd:" << rfd << ",任务:" << "printf log task" << endl
         << endl;
}
void ReloadConf(int rfd, pid_t pid)
{
    cout << "pid:" << pid << ",rfd:" << rfd << ",任务:" << "Reload conf task" << endl
         << endl;
}
void ConnectMysql(int rfd, pid_t pid)
{
    cout << "pid:" << pid << ",rfd:" << rfd << ",任务:" << "connect mysql task" << endl
         << endl;
}
uint32_t NextTask()
{
    return rand() % 3;
}

// 任务列表
task_t tasks[3] = {Printlog, ReloadConf, ConnectMysql};
void worker(int rfd)
{
    while (true)
    {
        uint32_t command_code = 0;
        ssize_t n = read(0, &command_code, sizeof(command_code));
        if (n == sizeof(command_code))
        {
            if (command_code >= 3)
            {
                continue;
            }
            tasks[command_code](rfd, getpid());
        }
        else if (n == 0)
        {
            cout << getpid() << ",进程执行结束" << endl;
            break;
        }
        sleep(1);
    }
}